package com.sunflower.bath.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.sunflower.framework.utils.JsonResult;
import org.sunflower.framework.utils.StringUtils;

import com.sunflower.bath.common.factory.JobFactory;
import com.sunflower.bath.common.interf.IJob;
import com.sunflower.bath.common.modle.ResultMsg;
import com.sunflower.bath.model.JobNeedExecut;

@Controller
@RequestMapping(value = "job")
public class JobController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@RequestMapping(value = "/addjob", method = { RequestMethod.GET })
	public JsonResult addJob(ModelMap map,JobNeedExecut jobNeedExecut,HttpServletRequest request,HttpServletResponse response) {
		JsonResult jr = JsonResult.newInstance();
		if(StringUtils.isNullOrBlank(jobNeedExecut.getJobName())){
			logger.error("任务名称不能为空");
			return jr.declareFailure("任务名称不能为空");
		}
		if(StringUtils.isNullOrBlank(jobNeedExecut.getJobDec())){
			logger.error("任务描述不能为空");
			return jr.declareFailure("任务描述不能为空");
		}
		if(StringUtils.isNullOrBlank(jobNeedExecut.getJobType())){
			logger.error("任务类型不能为空");
			return jr.declareFailure("任务类型不能为空");
		}
		if(StringUtils.isNullOrBlank(jobNeedExecut.getTimerType())){
			logger.error("任务跑批时间类型为空");
			return jr.declareFailure("任务跑批时间类型为空");
		}
		IJob job = JobFactory.getInstance(jobNeedExecut);
		ResultMsg resultMsg  = job.addJob(jobNeedExecut);
		return resultMsg.getIsSuccess()?jr.declareSuccess():jr.declareFailure(resultMsg.getErrorMsg());
	}
	
	
}
